
package com.yellow.aichatservice.config;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import com.alibaba.cloud.ai.memory.jdbc.PostgresChatMemoryRepository;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class BailianAutoconfiguration {
    // 优化后的系统提示词
//    private static final String retrievalSystemTemplate = """
//            使用以上下文来回答用户的问题。如果你不知道答案，就说你不知道。总是使用中文回答。
//
//            参考的上下文：
//            ···
//            {question_answer_context}
//            ···
//            如果给定的上下文无法让你做出回答，请回答数据库中没有这个内容。
//            有用的回答:""";

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;
    private static final String retrievalSystemTemplate = "你是一个友好的AI助手，可以根据用户的问题进行回答!";
    /**
     * 百炼调用时需要配置 DashScope API，对 dashScopeApi 强依赖。
     * @return DashScopeApi 实例
     */
    @Bean
    public DashScopeApi dashScopeApi() {
        return DashScopeApi.builder()
                .apiKey(apiKey)
                .build();
    }
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder,DocumentRetriever documentRetriever,ChatMemory chatMemory) {
        return builder
                .defaultAdvisors(
                        new DocumentRetrievalAdvisor(documentRetriever, new SystemPromptTemplate(retrievalSystemTemplate)),
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build())
                .defaultOptions(DashScopeChatOptions.builder().withModel("qwen3-max").build())
                .build();

    }
    @Bean
    public DocumentRetriever documentRetriever(DashScopeApi dashScopeApi) {
        return new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder().withIndexName("微服务").build());
    }

    @Bean
    public ChatMemory initChatMemory(JdbcTemplate jdbcTemplate) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(PostgresChatMemoryRepository.postgresBuilder()
                        .jdbcTemplate(jdbcTemplate)
                .build())
                .maxMessages(100)
                .build();


    }
}
